Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Collections
Imports System.Runtime.InteropServices
Imports System.Configuration
Imports System.Text
Public Class AuditEvent
    Public SESSION_USER_ID As String
    Public SESSION_GAPORTAL_ID As Integer = 0
    Public SESSION_COMPANY_ID As Integer = 0
    Public AFFECTED_USER_ID As String = "N.A"
    Public AFFECTED_GAPORTAL_ID As String = "N.A"
    Public AFFECTED_COMPANY_ID As Integer
    Public EventID As Integer = 0
    Public DBConnectString As String

    Private Conn As SqlConnection
    Private Cmd As SqlCommand
    Private AUDITEDINFO As New Hashtable
    Private sSQL As String
    Private AuditID As Long = 0
    Private ErrMsg As String
    Private sReturn As String = ""

    'Private Sub Class_Initialize()
    '    AFFECTED_COMPANY_ID = 0
    '    AFFECTED_USER_ID = "N.A"
    '    AFFECTED_VIAKLIX_ID = "N.A."
    '    EventID = 0
    '    sReturn = ""
    '    AuditID = 0
    'End Sub

    Public Sub AddInfo(ByVal nAuditFieldTypeID As Long, ByVal sData As String)
        Dim AUDITEDINFO As New Hashtable
        If IsNumeric(nAuditFieldTypeID) And nAuditFieldTypeID > 0 Then
            If Not IsNothing(sData) Then
                '-----  Preparing sData for database insertion.
                sData = Trim(Replace(sData, "'", "''"))
            End If
            If IsNothing(sData) Or Len(sData) = 0 Then
                sData = " "
            End If
            AUDITEDINFO.Add(nAuditFieldTypeID, sData)
            'AUDITEDINFO.Key = nAuditFieldTypeID
            'AUDITEDINFO.Value = sData
            'AUDITEDINFO.Add(nAuditFieldTypeID, sData)
        End If
        'Exit Sub
        'ErrorHandler:
        ' Call LogErrorEvent(".AddInfo")

    End Sub

    Public Function SaveEvent() As String
        Dim objEntry As DictionaryEntry
        Dim Conn As SqlConnection = New SqlConnection(DBConnectString)

        Dim sErrorMsg As String
        'Dim Dictionary_Key As Integer
        Dim Audit_Key As Integer
        'Dim strKey As String
        'Dim strValue As String
        sErrorMsg = ""
        Conn.Open()
        Dim Cmd As SqlCommand = New SqlCommand("INT_Audit", Conn)
        Cmd.CommandType = CommandType.StoredProcedure
        'With Cmd
        '    .Parameters.Add("@O_AUDIT", Nothing)
        '    .Parameters(0).SqlDbType = SqlDbType.Int
        '    .Parameters(0).Direction = ParameterDirection.Output

        '    .Parameters.Add("@I_EVENTID", CInt(EventID))
        '    .Parameters.Add("@I_SESS_GID", CInt(SESSION_GAPORTAL_ID))
        '    .Parameters.Add("@I_SESS_CID", CInt(SESSION_COMPANY_ID))
        '    .Parameters.Add("@I_SESS_UID", CStr(SESSION_USER_ID))
        '    .Parameters.Add("@I_AFF_GID", CInt(AFFECTED_GAPORTAL_ID))
        '    .Parameters.Add("@I_AFF_CID", CInt(AFFECTED_COMPANY_ID))
        '    .Parameters.Add("@I_AFF_UID", CStr(AFFECTED_USER_ID))
        '    .Parameters.Add("@0_ERRORMSG", Nothing)
        '    .Parameters(8).SqlDbType = SqlDbType.NVarChar
        '    .Parameters(8).Direction = ParameterDirection.Output

        'End With
        Try

            Cmd.ExecuteNonQuery().ToString()
            If Cmd.Parameters("0_ERRORMSG").Value = "" And IsNothing(Cmd.Parameters("0_ERRORMSG").Value) Then
                If Cmd.Parameters("O_AUDIT").Value > 0 Then
                    AuditID = Cmd.Parameters("O_AUDIT").Value
                Else
                    AuditID = 0
                End If
            Else
                AuditID = -1
                LogOracleError("AuditEvent.SaveEvent", Cmd.Parameters("0_ERRORMSG").Value)

                ErrMsg = Cmd.Parameters("0_ERRORMSG").Value
                ''ErrMsg = "Create New User Failed: " & Cmd.Parameters("o_error").Value & ""
                LogErrorEvent(ErrMsg)
                Return "-1"
            End If
            Conn.Close()

        Catch ex As SqlException
            Dim strErrEx As String = ""
            Dim strTDErr As String
            Dim i As Integer = 0

            For i = 0 To ex.Errors.Count - 1
                strTDErr = ex.Errors(i).Message.ToString()
            Next i
            ''/**use this code to debug**//

            'LogErrorEvent(strTDErr)

        Finally
            If Not (Conn Is Nothing) Then
                Conn.Close()
            End If
        End Try


        If AuditID > 0 Then
            '-----  AUDITS record successfully added.
            '-----  Saving audited information to AUDITED_INFO, if any
            '       data was tagged to be saved.
            'if AUDITEDINFO.
            'If AUDITEDINFO.Count > 0 Then
            For Each objEntry In AUDITEDINFO
                'Dictionary_Key = objEntry.Key 'Key
                Audit_Key = CInt(Right(CStr(objEntry.Key), 2))
                If Not IsNothing(Audit_Key) And CInt(objEntry.Key) > 0 Then
                    sSQL = ""
                    sSQL = sSQL & "insert into AUDITED_INFO "
                    sSQL = sSQL & "( AUDIT_ID, AUDIT_FIELD_ID, DATA ) values "
                    sSQL = sSQL & "( " & AuditID & ", " & Audit_Key & ", '" & objEntry.Key & "' )"
                    CallExecuteSQL(DBConnectString, sSQL, sErrorMsg)
                Else

                End If
            Next

        Else
     end if
        '-----  AUDITS record not successfully added.
        'App.LogEvent(vbCrLf & "AUDITS Record Not Added: " & vbCrLf & "EventID: " & EventID)

        SaveEvent = AuditID
        Conn.Close()
        Conn = Nothing
        Exit Function
        'ErrorHandler:
        '        Call LogErrorEvent(".SaveEvent")

        '        If Not Conn Is Nothing Then
        '            If Conn.State <> adStateClosed Then
        '                Conn.Close()
        '            End If

        '            Conn = Nothing
        '        End If

        '        Cmd = Nothing
    End Function

    Private Sub LogErrorEvent(ByVal sOrigin As String)
        Dim sErrMsg As String
        sErrMsg = ""
        sErrMsg = sErrMsg & "viaAudit.AuditEvent" & sOrigin & vbCrLf
        sErrMsg = sErrMsg & "Source: " & Err.Source & vbCrLf
        sErrMsg = sErrMsg & "Number: " & Err.Number & vbCrLf
        sErrMsg = sErrMsg & "Description: " & Err.Description & vbCrLf

        sErrMsg = sErrMsg & "------------------------------------" & vbCrLf

        sErrMsg = sErrMsg & "SessionCompanyID: " & SESSION_COMPANY_ID & vbCrLf
        sErrMsg = sErrMsg & "SessionviaKLIXID: " & SESSION_GAPORTAL_ID & vbCrLf
        sErrMsg = sErrMsg & "SessionUserID: " & SESSION_USER_ID & vbCrLf

        sErrMsg = sErrMsg & "AffectedCompanyID: " & AFFECTED_COMPANY_ID & vbCrLf
        sErrMsg = sErrMsg & "AffectedviaKLIXID: " & AFFECTED_GAPORTAL_ID & vbCrLf
        sErrMsg = sErrMsg & "AffectedUserID: " & AFFECTED_USER_ID & vbCrLf

        sErrMsg = sErrMsg & "EventID: " & EventID

        'App.LogEvent(sErrMsg, vbLogEventTypeError)
        'Debug.Print(sErrMsg)
    End Sub

    Public Function LogOracleError(ByVal sOrigin As String, ByVal sMessage As String) As String
        Dim sErrMsg As String
        sErrMsg = ""
        sErrMsg = sErrMsg & "viaAudit." & sOrigin & vbCrLf
        sErrMsg = sErrMsg & "Source: ORACLE" & vbCrLf
        sErrMsg = sErrMsg & "Description: " & sMessage & vbCrLf
        sErrMsg = sErrMsg & "------------------------------------" & vbCrLf

        sErrMsg = sErrMsg & "SessionCompanyID: " & SESSION_COMPANY_ID & vbCrLf
        sErrMsg = sErrMsg & "SessionviaKLIXID: " & SESSION_GAPORTAL_ID & vbCrLf
        sErrMsg = sErrMsg & "SessionUserID: " & SESSION_USER_ID & vbCrLf

        sErrMsg = sErrMsg & "AffectedCompanyID: " & AFFECTED_COMPANY_ID & vbCrLf
        sErrMsg = sErrMsg & "AffectedviaKLIXID: " & AFFECTED_GAPORTAL_ID & vbCrLf
        sErrMsg = sErrMsg & "AffectedUserID: " & AFFECTED_USER_ID & vbCrLf

        sErrMsg = sErrMsg & "EventID: " & EventID
        Return sErrMsg
        'App.LogEvent(sErrMsg, vbLogEventTypeError)
        'Debug.Print(sErrMsg)
    End Function

End Class
